using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foothill.Bus;
using Foothill.Bus.Result;
using Foothill.Bus.Node;
using Foothill.Persistent.Command;
using Foothill.Core.FieldMapping;
using Foothill.Utils;

namespace GGFW.DataAccess.Constants
{
    /// <summary>
    /// 数据访问类的基类
    /// </summary>
    public class BaseDao
    {
        /// <summary>
        /// 解析查询实体结果
        /// </summary>
        /// <param name="a_Result">查询结果</param>
        /// <returns>数据列表，没有数据时返回 null</returns>
        protected List<T> parseQueryResult<T>(ResultQueryEntity a_Result)
        {
            //执行结果为空
            if (a_Result == null)
            {
                throw new Exception(Common.UNKNOW_DB_ERROR);
            }

            //执行命令不成功
            if (!a_Result.isSuccess())
            {
                throw new Exception(a_Result.getMessage());
            }

            //是否存在记录
            if (a_Result.Records != null && a_Result.Records.Count > 0)
            {
                List<T> dataList = new List<T>();
                AbstractResultBus.CopyProperties<T>(dataList, a_Result);
                MappingFieldEvaluator.ProcessList<T>(dataList);

                return dataList;
            }

            return null;
        }

        /// <summary>
        /// 保存或修改指定的事物总线
        /// </summary>
        /// <param name="transBus">事物总线</param>
        /// <returns>受影响的记录数</returns>
        protected int Save(TransactionBus transBus)
        {
            SaveOrUpdateCommand saveCommand = new SaveOrUpdateCommand(new SaveOrUpdateBus(transBus));
            ResultSaveOrUpdateBus result = saveCommand.execute() as ResultSaveOrUpdateBus;
            //执行结果为空
            if (result == null)
            {
                throw new Exception(DataAccess.Constants.Common.UNKNOW_DB_ERROR);
            }
            //执行命令不成功
            if (!result.isSuccess())
            {
                throw new Exception(result.getMessage());
            }

            //受影响记录数
            int affectRows = 0;
            if (result.Entities != null)
            {
                foreach (ResultSaveOrUpdateEntity r in result.Entities)
                {
                    affectRows += r.AffectRows;
                }
            }
            if (result.NativeSqls != null)
            {
                foreach (ResultNativeSqlDeleteUpdate rd in result.NativeSqls)
                {
                    affectRows += rd.AffectRows;
                }
            }
            return affectRows;
        }

        /// <summary>
        /// 获得符合条件的查询结果总数
        /// </summary>
        /// <param name="querySql">查询Sql语句</param>
        /// <returns>结果总数</returns>
        public int GetQueryCount(string querySql)
        {
            string sql = string.Format("select count(0) as RecordCount from({0}) as counttmp", querySql);


            //设置查询企业基本信息总线
            NativeSqlBus queryBus = new NativeSqlBus("", sql);

            //执行查询业务代码命令
            QueryNativeSqlCommand queryCommand = new QueryNativeSqlCommand(queryBus);
            ResultQueryNativeSql result = queryCommand.execute() as ResultQueryNativeSql;

            //执行结果为空
            if (result == null)
            {
                throw new Exception(Common.UNKNOW_DB_ERROR);
            }

            //执行命令不成功
            if (!result.isSuccess())
            {
                throw new Exception(result.getMessage());
            }

            //是否存在记录
            if (result.Records != null && result.Records.Count > 0 && result.Records[0].Fields.Count > 0)
            {
                int count = NumericUtil.toInteger(result.Records[0].Fields[0].FieldValue, -1);

                return count;
            }

            return -1;
        }
    }
}
